<template>
  <div class="product-table">
    <el-card class="search-card" v-show="showSearch">
      <div class="form-wrap">
        <el-form @submit.native.prevent :model="queryParams" ref="queryForm" :inline="true" label-width="46px">
          <el-form-item>
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-form>
        <div class="search-btn-group">
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('search') }}</el-button>
          <el-button class="button-ty" icon="el-icon-refresh" @click="resetQuery">{{ $t('reset') }}</el-button>
        </div>
      </div>
    </el-card>

    <el-card class="table-card" style="border-radius: 0px">
      <el-row :gutter="10" style="margin-bottom: 16px">
<!--        <el-col :span="1.5">-->
<!--          <el-button plain icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>-->
<!--        </el-col>-->
<!--        <el-col :span="1.5">-->
<!--          <el-button plain icon="el-icon-edit" size="small" :disabled="single" @click="handleUpdate">修改</el-button>-->
<!--        </el-col>-->
<!--        <el-col :span="1.5">-->
<!--          <el-button plain icon="el-icon-delete" size="small" :disabled="multiple" @click="handleDelete">删除</el-button>-->
<!--        </el-col>-->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
      </el-row>
      <div class="data-table">
        <el-table v-loading="loading" :data="tableData" :row-key="getRowKey" :border="false" ref="multipleTable" :max-height="495">
          <el-table-column prop="deviceSipId" label="设备ID" align="center"></el-table-column>
          <el-table-column prop="channelSipId" label="通道ID" align="center"></el-table-column>
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sip_gen_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="是否显示" align="center" prop="shown">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.shown" active-value="0" inactive-value="1" @change="handleSwitchChange(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="channelName" label="通道名称" align="center"></el-table-column>
          <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="180">
            <template slot-scope="scope">
              <el-button size="small" type="text" icon="el-icon-setting" @click="handleDesign(scope.row)">配置</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination style="margin-bottom: 20px" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </div>
    </el-card>

    <el-dialog class="form-dialog" :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="视频流URL" prop="rtspUrl">
          <el-input v-model="form.rtspUrl" placeholder="请输入视频流URL" style="width: 400px" />
        </el-form-item>
        <el-form-item label="边缘盒子SN" prop="deviceSn">
          <el-input v-model="form.deviceSn" placeholder="请输入边缘盒子SN" style="width: 400px" />
        </el-form-item>
        <el-form-item label="算法" prop="algorithmIds">
          <el-select v-model="form.algorithmIds" multiple placeholder="请选择算法" style="width: 400px" class="select-box">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.eventName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确认</el-button>
        <el-button @click="cancel">{{ $t('cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCameras, shownData, getEventConfigList, deviceAlgorithm, deviceAlgorithmXq } from '@/api/private/configurationManagement';
export default {
  name: "videoTable",
  dicts: ['video_type', 'channel_type', 'sip_gen_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      tableData: [],
      dateRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: 1,
      },
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      options: [],
      rules: {
        rtspUrl: [{ required: true, message: "请输入视频流URL", trigger: 'blur' }],
        deviceSn: [{ required: true, message: "请输入边缘盒子SN", trigger: 'blur' }],
        algorithmIds: [{ required: true, message: "请选择算法", trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true;
      getCameras(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.loading = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        channelSipId: null,
        algorithmIds: [],
        rtspUrl: null,
        deviceSn: null,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm('queryForm');
      this.handleQuery();
    },
    //控制显隐
    handleSwitchChange(row) {
      const data = {
        tenantId: 1,
        shownValue: row.channelSipId,
        showType: "2",
        shown: row.shown,
      };
      shownData(data).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    // 查询安全事件列表
    getEventConfigData() {
      const queryParamsA = {
        pageNum: 1,
        pageSize: 999,
        tenantId: 1,
      }
      getEventConfigList(queryParamsA).then((response) => {
        this.options = response.rows;
      });
    },
    /** 配置按钮操作 */
    handleDesign(row) {
      this.reset();
      if (!row || row.channelSipId === undefined || row.channelSipId === null || row.channelSipId === "") {
      } else {
        deviceAlgorithmXq(row.channelSipId).then((response) => {
          if (response.data !== undefined && response.data !== null) {
            this.form = response.data;
          }
        }).catch((error) => {
          console.error("请求失败：", error);
          this.form = {};
        });
      }
      this.open = true;
      this.title = "配置视频算法";
      this.form.channelSipId = row.channelSipId;
      this.getEventConfigData();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          deviceAlgorithm(this.form).then((response) => {
            this.$modal.msgSuccess("配置成功");
            this.open = false;
            this.getList();
          });
          // if (this.form.id != undefined) {
          //   deviceAlgorithm(this.form).then((response) => {
          //     this.$modal.msgSuccess(this.$t('updateSuccess'));
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
          //   deviceAlgorithm(this.form).then((response) => {
          //     this.$modal.msgSuccess(this.$t('addSuccess'));
          //     this.open = false;
          //     this.getList();
          //   });
          // }
        }
      });
    },
    getRowKey(row){
      return row.id;
    },
  },
}
</script>

<style lang="scss" scoped>
.search-card {
  background: rgba(0,11,61,0.8);
  color: #fff;
  border: none;
  margin-bottom: 15px;
  padding: 3px 0;

  .form-wrap {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: flex-end;
    //margin-bottom: -22.5px;
    .search-btn-group {
      display: flex;
      flex-direction: row;
      float: right;
      margin-bottom: 22px;
    }
    ::v-deep .el-input__inner {
      background: rgba(255, 255, 255, 0.1);
      border-color: rgba(22, 93, 255, 0.5);
      color: #fff; /* 文字颜色 */
    }
    ::v-deep .el-range-editor--medium .el-range-input {
      background: none;
      color: #fff; /* 文字颜色 */
    }
  }
}

.button-ty {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(22, 93, 255, 0.5);
  color: #fff; /* 文字颜色 */
}

.table-card {
  background: rgba(0,11,61,0.8);
  border: none;
}

.data-table {
  ::v-deep .el-table {
    border: none !important;
  }

  ::v-deep .el-table__fixed-right-patch {
    height: 0 !important;
  }

  /* 2. 表头底下线条 */
  ::v-deep .el-table th.el-table__cell.is-leaf {
    border-bottom: none;
  }

  /* 2. 表头背景（列标题区域） */
  ::v-deep .el-table tr {
    background: rgba(22,93,255);
  }
  /* 2. 表头字体大小（列标题区域） */
  ::v-deep .el-table th.el-table__cell>.cell {
    //font-size: px2font(16);
    //border-right: none !important; /* 移除单元格右侧边框 */
    //border-left: none !important; /* 移除单元格左侧边框（通常默认无，但保险起见） */
  }

  /* 3. 表格体背景（数据行区域） */
  /* 3.1 普通行背景 */
  ::v-deep .el-table__body-wrapper,
  ::v-deep .el-table__row {
    background: rgba(0,11,61,0.79) !important; /* 普通行背景（示例：白色） */
  }
  /* 3.1 普通行字体大小 */
  ::v-deep .el-table td.el-table__cell div {
    //font-size: px2font(14);
  }
  /* 表格体单元格背景（确保与行背景一致） */
  ::v-deep .el-table__cell {
    //background-color: inherit !important; /* 继承行背景 */
    background: rgba(0,11,61,0.79) !important; /* 表格体文字颜色（可选） */
    border-right: none !important; /* 移除单元格右侧边框 */
    border-left: none !important; /* 移除单元格左侧边框（通常默认无，但保险起见） */
    border: 1px solid rgba(255,255,255,0.1);
    color: rgba(255,255,255,0.9);
  }

  /* 4. 行 hover 状态背景（鼠标悬停时） */
  ::v-deep .el-table__row:hover {
    background: rgba(22,93,255,0.4) !important; /* hover 背景（示例：淡蓝） */
  }

  /* 5. 表格边框颜色（可选：与背景色协调） */
  ::v-deep .el-table__border,
  ::v-deep .el-table-cell {
    background: rgba(0,11,61,0.79) !important; /* 边框颜色（示例：浅灰） */
  }

  ::v-deep .el-table--border::after, .el-table--group::after {
    width: 0 !important;
  }

  ::v-deep .el-table__fixed-right::before {
    height: 0 !important;
  }

  ::v-deep .el-table::before {
    height: 0 !important;
  }
  ::v-deep .el-button--text {
    color: #ffffff;
  }

  /* 1. 分页组件整体文字颜色（基础设置） */
  ::v-deep .el-pagination {
    color: #ffffff !important; /* 默认文字颜色（示例：深灰色） */
  }

  ::v-deep input.el-input__inner {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(22, 93, 255, 0.5);
    color: #ffffff;
  }

  ::v-deep button.btn-prev {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(22, 93, 255, 0.5);
    color: #ffffff;
  }

  ::v-deep button.btn-next {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(22, 93, 255, 0.5);
    color: #ffffff;
  }

  /* 2. 页码按钮文字颜色 */
  ::v-deep .el-pager li {
    color: #ffffff !important; /* 普通页码文字 */
    background: rgba(255, 255, 255, 0.1) !important;
    border-color: rgba(22, 93, 255, 0.5) !important;
  }

  /* 3. 当前选中页码的文字颜色 */
  ::v-deep .el-pager li.active {
    background-color: #409EFF !important;
    color: #ffffff !important; /* 选中页码文字（通常与背景色对比） */
  }

  /* 4. 上一页/下一页按钮文字颜色 */
  ::v-deep .el-pagination__prev,
  ::v-deep .el-pagination__next {
    color: #ffffff !important; /* 箭头按钮文字 */
  }

  /* 5. 禁用状态（如第一页的"上一页"）的文字颜色 */
  ::v-deep .el-pagination__prev.is-disabled,
  ::v-deep .el-pagination__next.is-disabled {
    color: #ffffff !important; /* 禁用状态文字（示例：浅灰色） */
  }

  /* 6. 总条数/跳转文本的颜色（如"共 100 条"） */
  ::v-deep .el-pagination__total,
  ::v-deep .el-pagination__jump {
    color: #ffffff !important; /* 说明文字颜色 */
  }

  ::v-deep .pagination-container {
    background: none;
    margin-top: px2vh(10);
    margin-bottom: px2vh(20);
  }

  ::v-deep .el-input--medium .el-input__inner {
    height: 28px;
  }
}

::v-deep .el-dialog {
  margin-top: 8vh !important;
  background: rgba(0,11,61);
}
.form-dialog {
  ::v-deep .el-input__inner {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(22, 93, 255, 0.5);
    color: #fff; /* 文字颜色 */
  }
  ::v-deep .el-dialog__title {
    color: #ffffff;
  }
  ::v-deep .el-dialog__headerbtn .el-dialog__close {
    color: #ffffff;
  }
  ::v-deep .el-form-item__label {
    color: #ffffff;
  }
}

.select-box ::v-deep .el-select-dropdown {
  background: rgba(0,11,61, 0.9);
  border-image: linear-gradient(
      180deg,
      rgba(22, 93, 255, 0),
      rgba(22, 93, 255, 0.59),
      rgba(22, 93, 255, 1)
  ) 1 1;
}
.select-box ::v-deep .el-select-dropdown__item {
  color: white;
}

.select-box ::v-deep .el-select-dropdown__item.hover, .el-select-dropdown__item:hover {
  background-color: rgba(22, 93, 255, 0.9);
}
</style>
